/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.infrastructure.adapters;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itgrail.pilot.domain.user.contant.OkrConstants;
import com.itgrail.pilot.domain.user.entity.UserOkrAlignE;
import com.itgrail.pilot.domain.user.ports.UserOkrAlignPort;
import com.itgrail.pilot.infrastructure.converter.UserOkrAlignConverter;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.ObjectiveMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.UserOkrAlignMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.ObjectiveDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.UserOkrAlignDO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.itgrail.grail.ddd.annotation.infrastructure.Adapter;

import java.util.List;
import java.util.stream.Collectors;

@Adapter
public class UserOkrAlignAdapter implements UserOkrAlignPort {

    @Autowired
    private UserOkrAlignMapper mapper;

    @Autowired
    private ObjectiveMapper objectiveMapper;

    @Autowired
    private UserOkrAlignConverter converter;

    @Override
    public int add(UserOkrAlignE userOkrAlignE) {
        UserOkrAlignDO userOkrAlignDO = converter.entityToData(userOkrAlignE);
        return mapper.insert(userOkrAlignDO);
    }

    @Override
    public int delete(Integer okrId) {
        //更新对齐的O的数量
        QueryWrapper<UserOkrAlignDO> wrapper = new QueryWrapper<>();
        wrapper.eq("start_objective_id", okrId);
        List<UserOkrAlignDO> alignDOList = mapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(alignDOList)) {
            List<Integer> acceptIds = alignDOList.stream().map(UserOkrAlignDO::getAcceptObjectiveId).collect(Collectors.toList());
            List<ObjectiveDO> objectiveDOS = objectiveMapper.selectBatchIds(acceptIds);
            objectiveDOS.forEach(x -> {
                if (null != x.getAlignCount() && x.getAlignCount() > OkrConstants.ALIGN_COUNT) {
                    x.setAlignCount(x.getAlignCount() - 1);
                    objectiveMapper.updateById(x);
                }
            });
        }
        int delete = mapper.delete(wrapper);

        QueryWrapper<UserOkrAlignDO> acceptWrapper = new QueryWrapper<>();
        acceptWrapper.eq("accept_objective_id", okrId);
        mapper.delete(acceptWrapper);
        return delete;
    }

    @Override
    public int deleteById(Integer id) {
        return mapper.deleteById(id);
    }
}
